from selenium import webdriver
from bs4 import BeautifulSoup
import time
from time import sleep
from selenium.webdriver import ChromeOptions
from selenium.webdriver.chrome.options import Options
import selenium.webdriver.support.ui as ui
from selenium.webdriver import ActionChains
options=webdriver.ChromeOptions()
# 封装一个函数，用来判断属性值是否存在
def isElementPresent(self, value):
    try:
        element = self.find_element_by_xpath(value)
    # 原文是except NoSuchElementException, e:
    except NoSuchElementException as e:
        # 发生了NoSuchElementException异常，说明页面中未找到该元素，返回False
        return False
    else:
        # 没有发生异常，表示在页面中找到了该元素，返回True
        return True

perinfo={
    # 登录账号
    'username':'',
    # 登录密码
    'password':'',
    # 数据集名字你创建数据集时起的名字
    'proname':'',
    #你选的地区，把对应的编号填入上面prolocation后面
    #eg:
    # 华北-北京四 填写1
    # 华东-上海一 填写2
    # 华南-广州 填写3
    'prolocation':''
}
options.add_experimental_option('excludeSwitches',['enable-automation'])
options.add_argument('user-agent="Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36"')
bro=webdriver.Chrome(options=options,executable_path='./chromedriver')
bro.maximize_window()
bro.get('https://auth.huaweicloud.com/authui/login.html?locale=zh-cn&service=https%3A%2F%2Fwww.huaweicloud.com%2F#/login')
bro.find_element_by_xpath('//*[@id="personalAccountInputId"]/input').send_keys(perinfo['username'])
bro.find_element_by_xpath('//*[@id="personalPasswordInputId"]/input').send_keys(perinfo['password'])
bro.find_element_by_xpath('//*[@id="btn_submit"]').click()
sleep(3)
bro.find_element_by_xpath('//*[@id="header-navigation-search-input"]').send_keys('modelarts')
bro.find_element_by_xpath('//*[@id="header"]/div/div[3]/div[1]/div[1]/div/div/div/ul/li[1]/form/button').click()
# 如果上一行代码一直报错的话，把上面的一行注释掉，换成下面这行
# bro.find_element_by_class_name('header-search-submit').click()
windows = bro.window_handles
bro.switch_to.window(windows[1])
bro.refresh()
time.sleep(0.7)
bro.find_element_by_xpath('//*[@id="content"]/section/div/div[1]/div/div/div[2]/div/div/div[1]/div/div/div/div/div[3]/a[2]').click()
windows = bro.window_handles
bro.switch_to.window(windows[-1])
bro.refresh()
time.sleep(0.7)
bro.find_element_by_xpath('//*[@id="menu"]/div[1]/div[4]/cf-region/div/a[1]').click()
time.sleep(1)
bro.find_element_by_xpath('//*[@id="menu"]/div[1]/div[4]/cf-region/div/ul/li['+perinfo['prolocation']+']').click()
time.sleep(1)
bro.find_element_by_xpath('//*[@id="dataManagement"]/ti-leftmenu-item/div').click()
time.sleep(1)
bro.find_element_by_xpath('//*[@id="dataset"]').click()
time.sleep(10)
bro.find_elements_by_link_text(perinfo['proname'])[0].click()
time.sleep(3)
bro.find_element_by_xpath('//*[@id="service-content"]/ui-view/dataset-dashboard/div/div[1]/div[2]/div[1]/div[1]/span/button').click()
time.sleep(3)
bro.find_element_by_xpath('//*[@id="service-content"]/ui-view/object-detect/div/div/div[2]/div/div/div/div[1]/div/ul/li[4]/a').click()
time.sleep(15)
bro.find_element_by_xpath('//*[@id="service-content"]/ui-view/object-detect/div/div/div[2]/div/div/div/div[2]/div[4]/div/div/div/div[1]/div[3]/div/div/div[1]/div[1]/div[1]').click()

time.sleep(3)
bro.find_element_by_xpath('/html/body/div[1]/div/div/div[3]/button').click()

time.sleep(3)
# 获取还有多少张图片没确认
num=bro.find_element_by_xpath('//*[@id="anonation"]/div/div[1]/div/div[3]/span[2]').get_attribute('textContent')
num=int(num)
print(num)
from selenium.common.exceptions import NoSuchElementException

for i in range(num):
    if isElementPresent(bro,'/html/body/div[1]/div/div'):
        bro.find_element_by_xpath('/html/body/div[1]/div/div/div[3]/button[2]').click()
        # 这里是每一百张会弹出弹框，加载新的一批的图片，如果自己的网速特别快，可以调小
        time.sleep(15)
    else:
        while(isElementPresent(bro,'//*[@id="anonation"]/div/div[2]/div[2]/div/div[2]/button')==False):
            time.sleep()
        bro.find_element_by_xpath('//*[@id="anonation"]/div/div[2]/div[2]/div/div[2]/button').click()
        # 每次点击之间的空隙，如果这行一直报错，说明网速不好，网页还没加载出来就进行下一次循环了，可以适当增加括号里的值
        time.sleep(0.5)


